package com.jing.android.notes.receiver;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.RemoteInput;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.jing.android.notes.R;
import com.jing.android.notes.constant.Constants;
import com.jing.android.notes.constant.Keys;

public class NotificationReceiver extends BroadcastReceiver {

    /**
     * 用于处理通知快速回复功能
     */
    public static final String ACTION_REPLY = "com.jing.android.notes.notification.reply";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ACTION_REPLY.equals(intent.getAction())) {
            int notificationId = intent.getIntExtra(Keys.NOTIFICATION_ID, -1);
            Bundle bundle = RemoteInput.getResultsFromIntent(intent);
            String reply = bundle.getString(Keys.NOTIFICATION_REPLY);
            Notification notification = new NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_ID_REPLIES)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("测试回复内容")
                    .setRemoteInputHistory(new String[]{reply})
                    .setPriority(NotificationCompat.PRIORITY_MIN)
                    .build();
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //安卓8.0(API 26),必须设置channel
                NotificationChannel channel = notificationManager.getNotificationChannel(Constants.NOTIFICATION_CHANNEL_ID_NOTES);
                if (channel == null) {
                    channel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID_REPLIES, context.getString(R.string.notification_reply),
                            NotificationManager.IMPORTANCE_MIN);
                    notificationManager.createNotificationChannel(channel);
                }
            }
            notificationManager.notify("test", notificationId, notification);
        }
    }
}